package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.AcctBalanceQueryRequest;
import cc.rengu.igas.channel.cups.facade.response.AcctBalanceQueryResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * 余额查询
 *
 * @author Administrator
 */
public class CupsAcctBalanceQueryService extends BasePayService<AcctBalanceQueryRequest, AcctBalanceQueryResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, AcctBalanceQueryRequest acctBalanceQueryRequest,
                                             AcctBalanceQueryResponse acctBalanceQueryResponse) throws Exception {
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(acctBalanceQueryRequest, cupsField);
        cupsField.setMsgType("0200");
        cupsField.setProcCode("300000");
        if ("01".equals(acctBalanceQueryRequest.getTermType())) {
            cupsField.setConCode("02");
        } else {
            cupsField.setConCode("00");
        }
        cupsField.setLocalDate(acctBalanceQueryRequest.getTransDateTime().substring(0, 4));
        cupsField.setLocalTime(acctBalanceQueryRequest.getTransDateTime().substring(4));
        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        cupsFieldBean.createF60()
                .setF60_2_2(acctBalanceQueryRequest.getTermRead())
                .setF60_2_3(acctBalanceQueryRequest.getIcConCode())
                .setF60_2_5(acctBalanceQueryRequest.getTermType());
        //  104
        String piSubField = packF104Pi(acctBalanceQueryRequest.getTermInfo());
        if (piSubField != null) {
            cupsField.setTermAttachData(piSubField);
        }
        cupsFieldBean.build();
        ConvertUtil.convertInput(cupsFieldBean);

        //  调用 CUPS
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean);

        // 应答赋值
        // 如果有脚本也需要返回脚本
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, acctBalanceQueryResponse);
        acctBalanceQueryResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(acctBalanceQueryResponse.getRespCode())) {
            acctBalanceQueryResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            acctBalanceQueryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(acctBalanceQueryResponse);
    }
}
